<template>
  <view class="custom-tabbar">
    <view
      v-for="(item, index) in list"
      :key="index"
      class="tabbar-item"
      :class="{ 'tabbar-item-active': selected === index }"
      @click="switchTab(item, index)"
    >
      <image
        v-if="index === 0"
        :src="selected === index ? item.selectedIconPath : item.iconPath"
        class="home-icon"
      />
      <image
        v-else
        :src="selected === index ? item.selectedIconPath : item.iconPath"
        class="tabbar-icon"
      />
      <text class="tabbar-text">{{ item.text }}</text>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    selected: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      list: [
        {
          pagePath: "/pages/index/index",
          text: "小店",
          iconPath: "https://app-cangshu.oss-rg-china-mainland.aliyuncs.com/asssets/merchant/myStore.png",
          selectedIconPath: "https://app-cangshu.oss-rg-china-mainland.aliyuncs.com/asssets/merchant/myStoreActive.png",
        },
        {
          pagePath: "/pages/orderList/index",
          text: "订单",
          iconPath: "https://app-cangshu.oss-rg-china-mainland.aliyuncs.com/asssets/merchant/order.png",
          selectedIconPath: "https://app-cangshu.oss-rg-china-mainland.aliyuncs.com/asssets/merchant/orderActive.png",
        },
      ],
    };
  },
  methods: {
    switchTab(item, index) {
      if (this.selected === index) return;
      uni.switchTab({
        url: item.pagePath,
        success: () => {
          this.$emit("change", index);
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.custom-tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 168rpx;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background-color: #ffffff;
  z-index: 999;
}

.tabbar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.home-icon {
  width: 50rpx;
  height: 40rpx;
}
.tabbar-icon {
  width: 37px;
  height: 42px;
}

.tabbar-text {
  font-size: 26rpx;
  margin-top: 8px;
}

.tabbar-item-active .tabbar-text {
  color: #EF4C2A;
}
</style>
